package com.fk.regimen;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.fk.bean.Question;
import com.fk.service.DBService;
import com.fk.util.CalendarUtil;
import com.fk.util.DateTimeUtil;
import com.fk.util.SolarTermsUtil;
import com.fk.util.StringUtil;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.TextView;


public class TodayFragment extends Fragment {
	
	
	private TextView tv_lunar;
	/**
	 * 选择的日历
	 */
	private Calendar calSelected = Calendar.getInstance();
	
	/**
     * 用于记录当前处理的时间
     */
    private Calendar mCurrenCalendar;
  //农历节气
    private String solarMsg;
    
    private String tag = "china";
    
    private View todayLayout;
	
	
	public View onCreateView(LayoutInflater inflater,
			 ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		todayLayout = inflater.inflate(R.layout.today, container,false);
		
		
		initViews();
		showTime();
		
		
		
		
		return todayLayout;
	}


	
	//显示时间
	private void showTime() {

		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		//阳历日期
		String solar = sdf.format(date);
		//得到农历日期
		mCurrenCalendar = calSelected;
		String lunar = new CalendarUtil(calSelected).getDay();
		//得到节气
		String solarMsg2;
		solarMsg2 = new SolarTermsUtil(mCurrenCalendar).getSolartermsMsg();
		if (!StringUtil.isNullOrEmpty(solarMsg2)) {
			solarMsg = solarMsg2;
        } else{
        	
        	for(int i=1;i<=16;i++){
        		//差值天气
        		Date cha = DateTimeUtil.getDateBefore(date, i);
        		//将Date转化为Calendar,此时calSelected就是Calendar类型的
        		calSelected.setTime(cha);
                mCurrenCalendar = calSelected;
                String solarMsg3 = new SolarTermsUtil(mCurrenCalendar).getSolartermsMsg();
        		
                if(!StringUtil.isNullOrEmpty(solarMsg3)){
                	solarMsg = solarMsg3;
                	break;
                }
 	
        	}
        }
		Log.e(tag, solar+"   农历"+lunar+"   "+solarMsg+"节气");
		//在相应的控件上显示日期
		tv_lunar.setText(solar+"   农历"+lunar+"   "+solarMsg+"节气");
		
	}



	private void initViews() {
		// TODO Auto-generated method stub
		tv_lunar = (TextView) todayLayout.findViewById(R.id.tv_lunar);
		
	}

}
